<?php

namespace app\admin\controller;

use app\common\controller\BasicAdmin;
use app\admin\service\SchoolQrcode;
use app\common\model\SchoolModel;
use app\common\traits\SelectPage;

/**
 * Class School
 */
class School extends BasicAdmin
{
    use SelectPage;

    /**
     * 学校管理
     */
    protected $model = 'SchoolModel';

    public function index(){
        $this->title = '学校管理';
        return  $this->_query($this->model)
            ->like('name')
            ->page();
    }

    /**
     * 添加
     */
    public function add()
    {
        $this->title = '添加';
        return $this->_form($this->model, 'form');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $this->title = '编辑';
        return $this->_form($this->model, 'form');
    }

    /**
     * 删除
     */
    public function del()
    {
        $this->_delete($this->model);
    }

    /**
     * 学校更新二维码
     */
    public function update_qrcode(){
        if ($this->request->isPost()) {
            $school_id = explode(',',$_POST['school_id']);
            foreach ($school_id as $key=>$value){
                $result = SchoolQrcode::CreateQrcode($value);
                if ($result) {
                    SchoolModel::where('id',$value)
                        ->update(['qrcode' => $result]);
                }
            }
            $this->success('学校更新小程序二维码成功！');
        }
    }
    /**
     * 启用/关闭
     */
    public function upstatus(){
        $this->_save($this->model, [
            'status' => $this->request->request('status'),
        ],'id', ['id' => $this->request->request('id')]);
    }


}